package com.cwj.weakpassword;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * @author: Chenwujie
 * @date: 2021-04-08 10:52
 */
public class WeakPasswordCheckTask implements Runnable {
    public WeakPasswordCheckTask(String weakPassword, String encodedPassword) {
        this.weakPassword = weakPassword;
        this.encodedPassword = encodedPassword;
    }

    @Override
    public void run() {
        try{
            if(!Thread.currentThread().isInterrupted()){
                boolean matches = passwordEncoder.matches(weakPassword, encodedPassword);
                // 随机抛出异常
//                Random random = new Random();
//                int i = random.nextInt(10);
//                if(i == 7){
//                    System.out.println(i/0);
//                }
                if (matches) {
                    System.out.println("encodedPassword: \"" + encodedPassword + "\" is matched: " + weakPassword);
                }
            }else{

            }
        }finally {
            countDownLatch.countDown();
        }
    }

    private static PasswordEncoder passwordEncoder;
    private static CountDownLatch countDownLatch;
    private String weakPassword;
    private String encodedPassword;

    public static void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        WeakPasswordCheckTask.passwordEncoder = passwordEncoder;
    }

    public static void setCountDownLatch(CountDownLatch countDownLatch) {
        WeakPasswordCheckTask.countDownLatch = countDownLatch;
    }
}
